
import ParserError from "@/js/GraphParser/src/ParserError"
import { DefaultLayout, GraphNode } from "@/js/components/GraphNode"
import { GraphView } from "@/js/components/GraphView"
import * as InputLinkNode from "@/js/components/InputLinkNode"

export class For extends GraphNode {
    layout?: DefaultLayout
    load() {
        super.load()

        const layout = new this.DefaultLayout(this, this.graphView as GraphView)
        this.layout = layout
        this.appendChild(layout.container)

        
        const nodes: (InputLinkNodeOptions & { layout: 'left' | 'right', inputLinkNode: InputLinkNodes })[] = [
            {
                name: "count",
                desc: '目标值',
                model: "entry",
                layout: "left",
                placeholder: "目标值",
                inputLinkNode: "InputLinkNodeNumber"
            },
            {
                name: "step",
                desc: '步长',
                model: "entry",
                inputValue: 1,
                layout: "left",
                placeholder: "步长",
                inputLinkNode: "InputLinkNodeNumber"
            },
            {
                name: "startValue",
                desc: '初始值',
                model: "entry",
                inputValue: 0,
                layout: "left",
                placeholder: "初始值",
                inputLinkNode: "InputLinkNodeNumber"
            },
            {
                name: "direction",
                desc: '反向',
                model: "entry",
                layout: "left",
                inputLinkNode: "InputLinkNodeBoolean",
                showLinkNode: false,
                
            },
            {
                name: "forBody",
                desc: '循环体',
                model: "output",
                showInput: false,
                layout: 'right',
                inputLinkNode: "InputLinkNodeFlow"
            },
            {
                name: "index",
                desc: '索引',
                model: "output",
                showInput: false,
                layout: 'right',
                inputLinkNode: "InputLinkNodeNumber",
                linkMaxCount: 100000
            }
        ]

        nodes.forEach(node => {
            if (layout.main) {
                const layout_ = layout.main[node.layout]
                if (layout_) layout_.appendChild(
                    new (InputLinkNode as any)[node.inputLinkNode](this, this.graphView, node)
                )
            }

        })
    }
}

For.parser = ({ node, next, to, data }) => {
    if (to.name === 'index') return data.index
    data.index = "i_" + node.uuid

    const output = next(node.outputs.output)
    const body = next(node.outputs.forBody)
    const count = next(node.enters.count, node.userData.enters.count || 0)
    const step = next(node.enters.step, node.userData.enters.step || 1)
    const startValue = next(node.enters.startValue, node.userData.enters.startValue || 0)
    const direction = next(node.enters.direction, node.userData.enters.direction || false)

    // throw new ParserError(node.uuid, "！逻辑错误：目标值、步长、初始值，需要满足节点解析失败")
    
    if(direction) return `for (let ${data.index} = ${startValue}; ${data.index} >= ${count }; ${data.index}-=${step }) {${body}}${output}`
    return `for (let ${data.index} = ${startValue}; ${data.index} < ${count }; ${data.index}+=${step }) {${body}}${output}`
}
